package org.yanh.cart.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.yanh.cart.config.CartProperties;
import org.yanh.cart.domain.Cart;
import org.yanh.cart.domain.CartFormDTO;
import org.yanh.cart.domain.CartVO;
import org.yanh.cart.service.ICartService;
import org.yanh.common.security.UserContext;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "购物车相关接口",description = "购物车相关接口")
@RestController
@RequestMapping("/carts")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;
    private final CartProperties cartProperties;
    @Operation(summary = "添加商品到购物车",description = "添加商品到购物车")
    @PostMapping
    public void addItem2Cart(@Valid @RequestBody CartFormDTO cartFormDTO){
        cartService.addItem2Cart(cartFormDTO);
    }

    @Operation(summary = "更新购物车数据",description = "更新购物车数据")
    @PutMapping
    public void updateCart(@RequestBody Cart cart){
        cartService.updateById(cart);
    }

    @Operation(summary = "删除购物车中商品",description = "删除购物车中商品")
    @DeleteMapping("{id}")
    public void deleteCartItem(@Param ("购物车条目id")@PathVariable("id") Long id){
        cartService.removeById(id);
    }

    @Operation(summary = "查询购物车列表",description = "查询购物车列表")
    @GetMapping
    public List<CartVO> queryMyCarts(){

        Long userId = UserContext.getUser();
        System.out.println(userId);
        System.out.println(cartProperties.getMaxAmount());
        return cartService.queryMyCarts();
    }
    @Operation(summary = "批量删除购物车中商品",description = "批量删除购物车中商品")
    @DeleteMapping
    public void deleteCartItemByIds(@RequestParam("ids") List<Long> ids){

        cartService.removeByItemIds(ids);
    }
    @Operation(summary = "查询测试",description = "查询测试")
    @GetMapping("/queryItemTest")
    public String queryItemTest(){

        return cartService.queryItemTest();
    }

}
